/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.maze.configuration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.maze.behaviours.ICrossBehaviour;
import org.maze.behaviours.IFitnessBehaviour;
import org.maze.behaviours.IImprovementBehaviour;
import org.maze.behaviours.IMutateBehaviour;

/**
 *
 * @author Thomas
 */
public class IndividualConfiguration {
    private static final String CROSS_BEHAVIOUR_TAG = "CrossBehaviour";
    private static final String IMPROVMENT_BEHAVIOUR_TAG = "ImprovementBehaviour";
    private static final String MUTATION_BEHAVIOUR_TAG = "MutationBehaviour";
    private static final String FITNESS_BEHAVIOUR_TAG = "FitnessBehaviour";
    private static final String BEHAVIOUR_TAG = "Behaviour";
    private static final String ALPHA_VALUE_TAG = "AlphaValue";

    private ICrossBehaviour crossBehaviour=null;
    private ArrayList<IImprovementBehaviour> improvementBehaviours =new ArrayList<IImprovementBehaviour>();
    private ArrayList<IMutateBehaviour> mutateBehaviours =new ArrayList<IMutateBehaviour>();
    private IFitnessBehaviour fitnessBehaviour = null;
    private double alpha=0;

    private Document document = null;

    public IndividualConfiguration(String path) throws Exception{
        SAXBuilder sxb = new SAXBuilder();
        document = sxb.build(new File(path));
        // load improvement behaviours
        List improvements = document.getRootElement().getChild(IMPROVMENT_BEHAVIOUR_TAG).getChildren(BEHAVIOUR_TAG);
        for(Object o : improvements ){
            Element e =(Element)o;
            String value = e.getValue().trim();
            System.out.println("Loading : "+value);
            Class t = Class.forName(value);
            improvementBehaviours.add((IImprovementBehaviour)t.getConstructor().newInstance());
        }
        // load mutation behaviours
        List mutate = document.getRootElement().getChild(MUTATION_BEHAVIOUR_TAG).getChildren(BEHAVIOUR_TAG);
        for(Object o : mutate ){
            Element e =(Element)o;
            String value = e.getValue().trim();
            System.out.println("Loading : "+value);
            Class t = Class.forName(value);
            mutateBehaviours.add((IMutateBehaviour)t.getConstructor().newInstance());
        }
        // load fitness behaviour
        Class f = Class.forName(document.getRootElement().getChild(FITNESS_BEHAVIOUR_TAG).getChildText(BEHAVIOUR_TAG).trim());
        fitnessBehaviour = ((IFitnessBehaviour)f.getConstructor().newInstance());
        // load cross behaviour
        Class c = Class.forName(document.getRootElement().getChild(CROSS_BEHAVIOUR_TAG).getChildText(BEHAVIOUR_TAG).trim());
        crossBehaviour = ((ICrossBehaviour)c.getConstructor().newInstance());
        // we get the alpha parameter for fitness
        alpha = Double.parseDouble(document.getRootElement().getChild(ALPHA_VALUE_TAG).getValue());
    }

    public ICrossBehaviour getCrossBehaviour(){
        return crossBehaviour;
    }

    public ArrayList<IImprovementBehaviour> getImprovementBehaviours(){
        return improvementBehaviours;
    }

    public ArrayList<IMutateBehaviour> getMutateBehaviours(){
        return mutateBehaviours;
    }

    public IFitnessBehaviour getFitnessBehaviour(){
        return fitnessBehaviour;
    }

    public double getAlpha(){
        return alpha;
    }
}
